package 简单.二叉树的最小绝对差;

import sun.reflect.generics.tree.Tree;

import java.util.Stack;

//非递归
public class Solution1 {
    public int getMinimumDifference(TreeNode root) {

        int min = Integer.MAX_VALUE;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode pre = null;

        while(cur != null || !stack.empty()){

            if(cur != null){
                stack.push(cur);
                cur = cur.left;
            }else{
                cur = stack.pop();
                if(pre != null){
                    min = Math.min(min,cur.val-pre.val);
                }
                pre =cur;
                cur = cur.right;
            }
        }
        return min;
    }

}
